package com.jike.noobmoney.mvp.view.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MoneyEntity;
import com.jike.noobmoney.entity.UserInfoEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.net.RetrofitHelper;
import com.jike.noobmoney.util.AppUtils;
import com.jike.noobmoney.util.ComUtils;
import com.jike.noobmoney.util.Constant;
import com.jike.noobmoney.util.Logger;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.SignUtil;
import com.jike.noobmoney.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;
import java.util.TreeMap;

/**
 * Create by xuke on 2019/3/11
 * 提现中心
 */
public class TiXianCenterActivityUpdate extends BaseActivity implements IView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_able_tixian)
    EditText tvAbleTixian;
    @BindView(R.id.tv_tixian)
    TextView tvTixian;
    @BindView(R.id.et_alipay_accout)
    TextView alipayEdit;
    @BindView(R.id.et_alipay_accout_name)
    EditText alipayEditName;

    @BindView(R.id.rg_box)
    RadioGroup rg_box;
    @BindView(R.id.rb_wechat)
    RadioButton rb_wechat;
    @BindView(R.id.rb_alipay)
    RadioButton rb_alipay;

    @BindView(R.id.rl_bind)
    RelativeLayout rl_bind;

    @BindView(R.id.rl_wx_bind)
    RelativeLayout rl_wx_bind;

    @BindView(R.id.wx_bind)
    TextView wx_bind;
    @BindView(R.id.et_wx_accout)
    TextView et_wx_accout;

    private UMShareAPI mShareAPI;

    public static final String TAG = TiXianCenterActivityUpdate.class.getSimpleName();


    private String userid;
    private String alipay_account;
    private String alipayaccountname;
    private UserPresenter userPresenter;
    private MoneyEntity.UserBean userBean;
    private  String accountName="";
    private String zhanghao="";


    @BindView(R.id.ll_right)
    TextView ll_right;
    public int type=0;
    int openidtype=0;

    public int flag=0;//0 代表选中支付宝  1代表选中微信

    @Override
    protected int layoutResID() {
        return R.layout.activity_tixian_center_update;
    }

    @Override
    protected void initData() {
        tvTitle.setText("提现");
        type=getIntent().getIntExtra("type",0);
        userPresenter = new UserPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        alipay_account=SPUtils.getInstance().getString(ConstantValue.SpType.alipayaccount);
        alipayaccountname=SPUtils.getInstance().getString(ConstantValue.SpType.alipayaccountname);
        alipayEdit.setText(alipay_account);
        alipayEditName.setText(alipayaccountname);
        refreshData();
        mShareAPI = UMShareAPI.get(this);
        //添加事件监听器
        rg_box.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(checkedId==R.id.rb_wechat){
                    rl_bind.setVisibility(View.GONE);
                    rl_wx_bind.setVisibility(View.VISIBLE);
                    flag=1;
                    if(openidtype==0){
                        wx_bind.setVisibility(View.VISIBLE);
                        et_wx_accout.setVisibility(View.GONE);
                    }else{
                        wx_bind.setVisibility(View.GONE);
                        et_wx_accout.setVisibility(View.VISIBLE);
                    }

                }
                else if(checkedId==R.id.rb_alipay){
                    rl_bind.setVisibility(View.VISIBLE);
                    rl_wx_bind.setVisibility(View.GONE);
                    flag=0;
                }

            }
        });
    }

    /**
     * 获取余额
     */
    private void refreshData() {
        showProgress();
        userPresenter.moneyView(userid, ConstantValue.RequestKey.getMoneyView);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private UMAuthListener mAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Log.e(TAG, "授权开始");
        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            String openid = map.get("openid");
            String wxnick = map.get("screen_name");
            bindWX(openid,wxnick);
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
            Log.e(TAG, "授权失败", throwable);
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            Log.e(TAG, "授权取消");
        }
    };

    /**
     * 获取个人信息
     */
    private void getData() {
        Call<UserInfoEntity>   userInfoCall = RetrofitHelper.getInstance().getUserInfo(userid);
        userInfoCall.enqueue(new Callback<UserInfoEntity>() {
            @Override
            public void onResponse(Call<UserInfoEntity> call, Response<UserInfoEntity> response) {
                if (response != null) {
                    UserInfoEntity body = response.body();
                    if (body != null && "100".equals(body.getCode())) {
                        UserInfoEntity.UserBean user = body.getUser();
                        if (user != null) {


                            String  alipayaccout=user.alipayaccount;
                            if(alipayaccout==null||alipayaccout.length()==0){
                                ll_right.setVisibility(View.VISIBLE);
                                } else {
                                alipayEdit.setText(alipayaccout);
                                ll_right.setVisibility(View.GONE);
                            }

                            openidtype =user.openidtype;
                            if(openidtype==0){
                                wx_bind.setVisibility(View.VISIBLE);
                                et_wx_accout.setVisibility(View.GONE);
                            }else{
                                wx_bind.setVisibility(View.GONE);
                                et_wx_accout.setVisibility(View.VISIBLE);
                                et_wx_accout.setText(user.wxnick);
                            }


                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<UserInfoEntity> call, Throwable t) {
                ToastUtils.showShortToastSafe(t.getMessage());
            }
        });
    }
    @Override
    public boolean ismain() {
        return true;
    }
    @Override
    public Boolean isfits() {
        return true;
    }

    @OnClick({R.id.iv_back, R.id.tv_tixian,R.id.tv_all_tx,R.id.ll_right,R.id.wx_bind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.wx_bind:
                //授权登录，回调成功，获取openid
                mShareAPI.getPlatformInfo(TiXianCenterActivityUpdate.this, SHARE_MEDIA.WEIXIN, mAuthListener);
                break;
            case R.id.ll_right:
                startActivity(new Intent(context, MyInfoActivityUpdate.class));
                break;
            case R.id.tv_all_tx:
                if(type==0) {
                if (userBean != null) {
                    if (userBean.getMoney()==0) {
                        ToastUtils.showShortToastSafe("可提现金额不足");
                        return;
                    }else
                    {
                        tvAbleTixian.setHint("可提金额为2的整数倍");
                    }
                }
                }else {
                    if (userBean != null) {
                        if (userBean.getRwmoney()==0) {
                            ToastUtils.showShortToastSafe("可提现金额不足");
                            return;
                        }else
                        {
                            tvAbleTixian.setHint("可提金额为2的整数倍");
                        }
                    }
                }
                break;
            case R.id.tv_tixian:
                if(type==0) {
                    if (AppUtils.isFastClick()) {
                        return;
                    }
                    if (userBean != null) {
                        if (userBean.getMoney() == 0) {
                            ToastUtils.showShortToastSafe("可提现金额不足");
                            return;
                        }
                        String money = tvAbleTixian.getText().toString();
                        if (money == null || money.length() == 0 || Double.parseDouble(money) > userBean.getMoney()||Double.parseDouble(money)%2==1) {
                            ToastUtils.showShortToastSafe("请输入正确的提现金额");
                            return;
                        }

//                    String accountName=alipayEditName.getText().toString();
//                    if(accountName==null||accountName.length()==0){
//                        ToastUtils.showShortToastSafe("请输入收款人姓名");
//                        return;
//                    }
                        tixian();
                    }
                }else
                {
                    if (AppUtils.isFastClick()) {
                        return;
                    }
                    if (userBean != null) {
                        if (userBean.getRwmoney() == 0) {
                            ToastUtils.showShortToastSafe("可提现金额不足");
                            return;
                        }
                        String money = tvAbleTixian.getText().toString();
                        if (money == null || money.length() == 0 || Double.parseDouble(money) > userBean.getRwmoney()||Double.parseDouble(money)%2==1) {
                            ToastUtils.showShortToastSafe("请输入正确的提现金额");
                            return;
                        }

//                    String accountName=alipayEditName.getText().toString();
//                    if(accountName==null||accountName.length()==0){
//                        ToastUtils.showShortToastSafe("请输入收款人姓名");
//                        return;
//                    }
                        rwtixian();
                    }


                }
                break;
        }
    }
    /**
     * 提现
     */
    private void tixian() {
        //提现,保存提现的时间
        //SPUtils.getInstance().put(ConstantValue.SpType.day, ComUtils.getDay());
        int anInt = SPUtils.getInstance().getInt(ConstantValue.SpType.cishu, 0);
        SPUtils.getInstance().put(ConstantValue.SpType.cishu, anInt + 1);

         zhanghao = alipayEdit.getText().toString().trim();
        accountName=alipayEditName.getText().toString();
        String money = tvAbleTixian.getText().toString().trim();


        if(flag==0) {
            //支付宝提现
            String account = alipayEdit.getText().toString();
            if (account == null || account.length() == 0) {
                ToastUtils.showShortToastSafe("请前往绑定支付宝账号");
                return;
            }
            showProgress();
        userPresenter.tixian(userid, money, ConstantValue.RequestKey.tixian);
        }else if(flag==1)
        {
            String account = et_wx_accout.getText().toString();
            if (account == null || account.length() == 0) {
                ToastUtils.showShortToastSafe("请前往绑定微信账号");
                return;
            }
            showProgress();
            //微信提现 weix
            userPresenter.rwwxtixian(userid, money, ConstantValue.RequestKey.tixian_wx);
        }

    }


    /**
     * 绑定
     */
    private void bindWX(String openid,String wxnick) {
        showProgress();
        TreeMap<String,String> paraMap = new TreeMap<String,String>();
        paraMap.put("openid",openid);
        paraMap.put("wxnick",wxnick);
        String sign= SignUtil.generateSignature(paraMap, Constant.VALUE);
        userPresenter.bindWx(openid,wxnick, sign, ConstantValue.RequestKey.bindwx);
    }


    /**
     * 任务提现
     */
    private void rwtixian() {
        //提现,保存提现的时间
        //SPUtils.getInstance().put(ConstantValue.SpType.day, ComUtils.getDay());
        int anInt = SPUtils.getInstance().getInt(ConstantValue.SpType.cishu, 0);
        SPUtils.getInstance().put(ConstantValue.SpType.cishu, anInt + 1);

        zhanghao = alipayEdit.getText().toString().trim();
        accountName=alipayEditName.getText().toString();
        String money = tvAbleTixian.getText().toString().trim();

        if(flag==0) {
            //支付宝提现
        String account = alipayEdit.getText().toString();
        if (account == null || account.length() == 0) {
            ToastUtils.showShortToastSafe("请前往绑定支付宝账号");
            return;
        }
            showProgress();
        userPresenter.rwtixian(userid, money, ConstantValue.RequestKey.rwtixian);
        }else if(flag==1) {
            //微信提现
            String account = et_wx_accout.getText().toString();
            if (account == null || account.length() == 0) {
                ToastUtils.showShortToastSafe("请前往绑定微信账号");
                return;
            }
            showProgress();
            userPresenter.tiwxxian(userid, money, ConstantValue.RequestKey.rwtixianwx);
        }

    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
            //获取用户余额
            userBean = (MoneyEntity.UserBean) data;
            if(type==0) {
                tvMoney.setText("" + userBean.getMoney());

                tvAbleTixian.setHint("可提金额为2的整数倍");
            }else
            {
                tvMoney.setText("" + userBean.getRwmoney());

                tvAbleTixian.setHint("可提金额为2的整数倍");
            }
        }
        if (ConstantValue.RequestKey.tixian.equals(actionType)) {
            SPUtils.getInstance().put(ConstantValue.SpType.alipayaccount, "" + zhanghao);
            SPUtils.getInstance().put(ConstantValue.SpType.alipayaccountname, "" + accountName);
            ToastUtils.showShortToastSafe(msg);
            finish();
        }

        if (ConstantValue.RequestKey.rwtixian.equals(actionType)) {
            SPUtils.getInstance().put(ConstantValue.SpType.alipayaccount, "" + zhanghao);
            SPUtils.getInstance().put(ConstantValue.SpType.alipayaccountname, "" + accountName);
            ToastUtils.showShortToastSafe(msg);
            finish();
        }
        if (ConstantValue.RequestKey.rwtixianwx.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            finish();
        }
        if (ConstantValue.RequestKey.tixian_wx.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            finish();
        }

        if (ConstantValue.RequestKey.bindwx.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            getData();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        ToastUtils.showShortToastSafe(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            Logger.e("xuke", "onActivityResult");
            refreshData();
        }
    }

}
